﻿var uri = '/KPT/SKpt.ashx';
var uid = '1234567890123456';
var did = ''; // 当前KPTID。
var u = 'u';
var o = 'o';
var ds = 'ds';
var dc = 'dc';

var kpt;

$(document).ready(function () {
    kpt = new Kpt('all');
    $('#dvAll').append(kpt.genInput());
    listKpts();
});

Date.prototype.pattern = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份           
        "d+": this.getDate(), //日           
        "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时           
        "H+": this.getHours(), //小时           
        "m+": this.getMinutes(), //分           
        "s+": this.getSeconds(), //秒           
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度           
        "S": this.getMilliseconds() //毫秒           
    };
    var week = {
        "0": "/u65e5",
        "1": "/u4e00",
        "2": "/u4e8c",
        "3": "/u4e09",
        "4": "/u56db",
        "5": "/u4e94",
        "6": "/u516d"
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    if (/(E+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[this.getDay() + ""]);
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}

var Kpt = {
    URI: '/KPT/SKpt.ashx',

    createNew: function () {
        var kpt = { oid: 0 };
        kpt.genInput = function (id) {
            kpt.oid = id;
            var obj = '<div id="' + id + '_t">';
            obj += '<input type="text" id="' + id + 'gtds" name="gtds" class="gtd_tx" onkeydown="return appendKpt(event)" placeholder="点击输入，回车继续" x-webkit-speech="" />';
            //obj += '<input type="button" id="' + id + 'gtdb" class="gtd_bt" onclick="return appendGtd()" />';
            obj += '</div>';

            obj += '<div id="' + id + '_d"></div>';

            return obj;
        }
        kpt.genItem = function () {
        }
    }
}
function Kpt() {
    this.URI = '/KPT/SKpt.ashx';

    this.oid = id;

    var _This = this;


    Kpt.prototype.genItem = function (kpt) {
        var obj = '<div id="dv' + kpt.Id + '" class="gtd_item">';
        obj += '<table width="100%" cellpadding="3" cellspacing="0">';
        obj += '<tr id="c' + kpt.Id + '">';
        obj += '<td class="gtd_s" align="center" valign="middle" rowspan="2">';
        obj += '<input type="checkbox" id="' + kpt.Id + '" value="' + kpt.Status + '" onclick="return changeStatus(this);" />';
        //obj += '<div id="' + gtd.Id + '" values="' + gtd.Status + '" class="gtd_cu" onclick="return changeStatus(this);"/>';
        obj += '</td>';
        obj += '<td class="gtd_c" align="left" colspan="2">';
        obj += kpt.Subject;
        obj += '</td>';
        obj += '</tr>';
        obj += '<tr id="o' + kpt.Id + '">';
        obj += '<td class="gtd_t" align="left">';
        obj += kpt.DisplayTime;
        obj += '</td>';
        obj += '<td class="gtd_o" align="right">';
        obj += '<input type="image" title="提醒" class="opt_def opt_hint" onclick="return editDate(\'' + kpt.Id + '\');" /> ';
        obj += '<input type="image" title="编辑" class="opt_def opt_edit" onclick="return editGtd(\'' + kpt.Id + '\');" /> ';
        obj += '<input type="image" title="进度" class="opt_def opt_tags" onclick="return editKpt(\'' + kpt.Id + '\');" /> ';
        obj += '<input type="image" title="标签" class="opt_def opt_tags" onclick="return editTag(\'' + kpt.Id + '\');" /> ';
        obj += '</td>';
        obj += '</tr>';
        obj += '</table>';
        obj += '</div>';

        return obj;
    }
}

Kpt.prototype.genInput = function (id) {
};

function appendKpt(e) {
    var evt = e || event;
    if (evt.keyCode != 13) {
        return evt;
    }
    var tx = $('#gtds');
    $.ajax({
        type: "POST",
        url: uri,
        data: { u: uid, o: "c", ds: tx.val() },
        dataType: 'json',
        success: function (dat) {
            //var gtd = eval(dat);
            //alert(dat);
            tx.val('');
            $('#tgtd').append(kpt.genItem(dat));
            tx.focus();
        },
        error: function () {
            alert('error');
        }
    });
    return false;
}

function listKpts() {
    $.ajax({
        type: "POST",
        url: uri,
        data: { u: uid, o: "l" },
        dataType: 'json',
        success: function (dat) {
            var obj = $('#tgtd');
            var i = 0;
            var n = dat.length;
            for (; i < n; i += 1) {
                obj.append(genItem(dat[i]));
            }
        },
        error: function () {
            alert('error');
        }
    });
}

function changeStatus(obj) {
    var st = obj.value;
    if (st != 0) {
        showEcho('OK');
        return;
    }

    $.ajax({
        type: "POST",
        url: uri,
        data: { u: uid, o: "u", di: obj.id, du: 1 },
        dataType: 'json',
        success: function (dat) {
            $('#dv' + obj.id).hide();
            showEcho('OK');
        },
        error: function () {
            alert('error');
        }
    });
}
function editDate(id) {
    $("#dgDate").dialog({
        resizable: false,
        height: 480,
        modal: true,
        buttons: {
            "保存": function () {
                $(this).dialog("close");
            },
            "取消": function () {
                $(this).dialog("close");
            }
        }
    });
    return false;
}
function editGtd(id) {
    $("#dgGtd").dialog({
        resizable: false,
        height: 480,
        modal: true,
        buttons: {
            "保存": function () {
                updateGtd(id);
            },
            "取消": function () {
                $(this).dialog("close");
            }
        }
    });
    return false;
}
function editKpt(id) {
    $("#dgKpt").dialog({
        resizable: false,
        height: 480,
        modal: true,
        buttons: {
            "保存": function () {
                updateKpt(id);
            },
            "取消": function () {
                $(this).dialog("close");
            }
        }
    });
    $("#dpKpt").accordion();
    return false;
}
function editTag(id) {
    $("#dgGtd").dialog({
        resizable: false,
        height: 480,
        modal: true,
        buttons: {
            "保存": function () {
                updateKpt(id);
            },
            "取消": function () {
                $(this).dialog("close");
            }
        }
    });
    return false;
}
function updateGtd(id) {
    var a = $('#ckdl').is(':checked') ? 1 : 0;
    alert(a);
    $.ajax({
        type: "POST",
        url: uri,
        data: { u: uid, o: "u", di: id, ds: $('#tads').val(), dc: $('#tadc').val(), dm: $('#ckdm').is(':checked') ? 1 : 0, dl: $('#ckdl').is(':checked') ? 1 : 0 },
        dataType: 'json',
        success: function (dat) {
            $("#dgGtd").dialog("close");
        },
        error: function () {
            alert('error');
        }
    });
}
function updateKpt(id) {
    $.ajax({
        type: "POST",
        url: uri,
        data: { u: uid, o: "k", di: id, dk: $('#taKptk').val(), dp: $('#taKptp').val(), dt: $('#taKptt').val() },
        dataType: 'json',
        success: function (dat) {
            $("#dgKpt").dialog("close");
        },
        error: function () {
            alert('error');
        }
    });
}

var dvMsg;
var opMsg;
var itMsg;
function showEcho(msg) {
    $('#llMsg').text(msg);
    dvMsg = $('#dvMsg');
    opMsg = 1.0;
    if (itMsg == null) {
        itMsg = setInterval(showEcho1, 100);
    }
}
function showEcho1() {
    //dvMsg.style.filter = 'alpha(opacity=' + opMsg + ') gray';
    dvMsg.css({ 'opacity': opMsg });
    opMsg -= 0.1;
    if (opMsg < 0.1) {
        clearInterval(itMsg);
        itMsg = null;
    }
}